package com.yjc.sunnyweather.ui.place

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import com.yjc.sunnyweather.MainActivity
import com.yjc.sunnyweather.R
import com.yjc.sunnyweather.extension.toast
import com.yjc.sunnyweather.ui.loading.CatLoadingView
import com.yjc.sunnyweather.ui.weather.WeatherActivity
import kotlinx.android.synthetic.main.fragment_place.*

class PlaceFragment : Fragment() {

    val viewModel by lazy { ViewModelProvider(this).get(PlaceViewModel::class.java) }

    private lateinit var adapter: PlaceAdapter

    private lateinit var mView: CatLoadingView

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        return inflater.inflate(R.layout.fragment_place , container , false)
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        val placeSave = viewModel.getSavedPlace()
        if (activity is MainActivity && placeSave != null){
            WeatherActivity.actionStart(requireContext(),placeSave.location.lng,placeSave.location.lat,placeSave.name)
            activity?.finish()
            return
        }

        val layoutManager = LinearLayoutManager(activity)
        placeRecyclerView.layoutManager = layoutManager
        adapter = PlaceAdapter(this,viewModel.placeList)
        placeRecyclerView.adapter = adapter
//        searchPlaceEdit.addTextChangedListener { editable ->
//            val content = editable.toString()
//            if (content.isNotEmpty()){
//                viewModel.searchPlaces(content)
//            }else{
//                placeRecyclerView.visibility = View.GONE
//                bgImageView.visibility = View.VISIBLE
//                viewModel.placeList.clear()
//                adapter.notifyDataSetChanged()
//            }
//
//        }

        placeBtn.setOnClickListener {

            val content = searchPlaceEdit.text.toString()
            if (content.isNotEmpty()){
                mView = CatLoadingView()
                //显示
                mView.show(parentFragmentManager, "");
                viewModel.searchPlaces(content)
            }else{
                placeRecyclerView.visibility = View.GONE
                bgImageView.visibility = View.VISIBLE
                viewModel.placeList.clear()
                adapter.notifyDataSetChanged()
            }

        }

        viewModel.placeLiveData.observe(viewLifecycleOwner) { result ->

            val places = result.getOrNull()
            viewModel.placeList.clear()
            if (places != null) {
                placeRecyclerView.visibility = View.VISIBLE
                bgImageView.visibility = View.GONE
                viewModel.placeList.addAll(places)
            } else {
                val msg = "未能查询到任何地点"
                msg.toast()
                placeRecyclerView.visibility = View.GONE
                bgImageView.visibility = View.VISIBLE
                result.exceptionOrNull()?.printStackTrace()
            }
            adapter.notifyItemRangeChanged(0, viewModel.placeList.size)
            //消失
            mView.dismiss();
        }
    }


}